package scau.mis4.chanyulin.atm.base.system;

import scau.mis4.chanyulin.atm.base.common.CODE;
import scau.mis4.chanyulin.atm.base.utils.TimeUtils;
import scau.mis4.chanyulin.atm.domain.Account;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chanyulin on 2018/5/24.
 * 数据库服务
 */
public class DbService {

    // 模拟数据库account表
    private static Map<String, Account> accountTable = new HashMap<String, Account>() {{
        put("A0001", new Account("5c2b7283-7d81-4ea5-a7a7-be1b92c882c9", "A0001", "123456", "陈昱霖", "男", TimeUtils.strToShortDate("1996-12-19"), 1000.0));
        put("A0002", new Account("a17f8c35-c05d-4320-8f16-fbcf839ebd81", "A0002", "abcdefg", "梅锦莲", "女", TimeUtils.strToShortDate("1997-03-30"), 500.0));
    }};

    public DbService() {
    }

    /**
     * 模拟数据库SELECT语句，通过卡id找到对应的银行账户
     * @param cardId
     * @return
     */
    public Account findAccount(String cardId) {
        return accountTable.get(cardId);
    }

    /**
     * 通过卡id获取账户余额
     * @param cardId
     * @return
     */
    public double getAccountBalance(String cardId) {
        Account account = findAccount(cardId);
        if (null == account) return -1;
        return findAccount(cardId).getBalance();
    }

    /**
     * 通过卡id更新账户余额
     * @param account
     * @param change
     * @return
     */
    public int updateBalance(Account account, double change) {

        if (null == account) return CODE.ERR_NULL_POINTER;

        double moneyLeft = account.getBalance();
        account.setBalance(moneyLeft + change);
        accountTable.put(account.getCardId(), account);

        return CODE.MSG_SUCCESS;
    }

}
